//
//  VCOfReadArticle.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/1.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfReadArticle.h"
#import "MJMacro.h"
#import "BaseCollectionView.h"
#import "CVCellOfSliderOfReadArticle.h"
#import "BaseButton.h"
#import "CVCellOfBelowReadArticle.h"
#import "CVCellOfSecondBelowReadArticle.h"
#import "MJRefresh.h"
@interface VCOfReadArticle ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSArray *buttonTitleArray;
@property (nonatomic, strong) BaseCollectionView *upsideCollectionView;
@property (nonatomic, assign) CGRect newRect;
@property (nonatomic, strong) NSMutableArray *mArrayRect;
@property (nonatomic, strong) UIView *redSliderView;
@property (nonatomic, strong) BaseCollectionView *belowCollectionView;
@property (nonatomic, strong) NSArray<NSString *> *arrayOfStringURL;

@end

@implementation VCOfReadArticle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 网址
    _arrayOfStringURL = @[
                          @"http://yiapi.xinli001.com/v2/yi/get-article-bytag?page=1&size=10&tag_type=1&tag_id=876&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=12ffde4bb24113e5143e0fcc7fd73783&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93",
                          @"http://yiapi.xinli001.com/v2/yi/get-article-bytag?page=1&size=10&tag_type=1&tag_id=862&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=09322f5c6456bd2f6e42b1b96074ca68&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93",
                          @"http://yiapi.xinli001.com/v2/yi/get-article-bytag?page=1&size=10&tag_type=1&tag_id=844&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=9237c7d4b58357acd4270140f0bf626e&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93",
                          @"http://yiapi.xinli001.com/v2/yi/get-article-bytag?page=1&size=10&tag_type=1&tag_id=733&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=632cac2e8614fea99aaa265d4a4bed90&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93",
                          @"http://yiapi.xinli001.com/v2/yi/get-article-bytag?page=1&size=10&tag_type=1&tag_id=792&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=84edbc41e012fe11f7aec1f8e48d0c21&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93",
                          @"http://yiapi.xinli001.com/v2/yi/get-article-bytag?page=1&size=10&tag_type=1&tag_id=823&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=542dee90115049e72aa829037d53e4cf&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93",
                          @"http://yiapi.xinli001.com/v2/yi/get-article-bytag?page=1&size=10&tag_type=1&tag_id=1309&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=4c187d4320d592b7bdbf915265b79439&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93"];
    //初始化数组
    _mArrayRect = [NSMutableArray array];
    // 创建上面的滑动条
    [self createSlider];
    // 创建中间小红条
    [self createRedSlider];
    // 创建下面的 CollectionView
    [self createBelowCollectionView];
}
#pragma mark - 创建上面的滑动条
- (void)createSlider{
    // 创建返回的 button
    BaseButton *backButton = [BaseButton buttonWithType:UIButtonTypeSystem];
    backButton.backgroundColor = [UIColor whiteColor];
    backButton.frame = CGRectMake(0, 20, 60, 44);
    [backButton setTitle:@"< 返回" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    _buttonTitleArray = @[@"全部", @"婚恋", @"成长", @"职场", @"性心理", @"科普", @"健康", @"短片·漫画"];
    // 创建上面的滑动块
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.upsideCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(60, 20, SCREEN_WIDTH - 60, 44) collectionViewLayout:layout];
    layout.itemSize = CGSizeMake(80, 40);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.upsideCollectionView.showsHorizontalScrollIndicator = NO;
    self.upsideCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.upsideCollectionView];
    self.upsideCollectionView.delegate = self;
    self.upsideCollectionView.dataSource = self;
    self.upsideCollectionView.bounces = NO;
    [self.upsideCollectionView registerClass:[CVCellOfSliderOfReadArticle class] forCellWithReuseIdentifier:@"CVCellOfSliderOfReadArticle"];
}
#pragma mark - 返回之前的界面
- (void)back:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - 创建中间的滑动小红条
- (void)createRedSlider{
    self.redSliderView = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 60, 4)];
    self.redSliderView.backgroundColor = [UIColor colorWithRed:0.38 green:0.65 blue:0.96 alpha:1.00];
    [self.upsideCollectionView addSubview:_redSliderView];
}
#pragma mark - 创建下面大的 CollectionView
- (void)createBelowCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.belowCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    self.belowCollectionView.pagingEnabled = YES;
    self.belowCollectionView.showsHorizontalScrollIndicator = NO;
    self.belowCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_belowCollectionView];
    self.belowCollectionView.delegate = self;
    self.belowCollectionView.dataSource = self;
    self.belowCollectionView.bounces = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.belowCollectionView registerClass:[CVCellOfBelowReadArticle class] forCellWithReuseIdentifier:@"CVCellOfBelowReadArticle"];
    [self.belowCollectionView registerClass:[CVCellOfSecondBelowReadArticle class] forCellWithReuseIdentifier:@"CVCellOfSecondBelowReadArticle"];
}
#pragma mark - layout 的代理方法,改变 item 的大小
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *string = _buttonTitleArray[indexPath.row];
   _newRect =  [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return _newRect.size.width;
}
#pragma mark - 返回一个分区的 item 的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   return _buttonTitleArray.count;
}
#pragma mark - Collec cell 赋值
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([collectionView isEqual:_upsideCollectionView]) {
    CVCellOfSliderOfReadArticle *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfSliderOfReadArticle" forIndexPath:indexPath];
    NSString *string = _buttonTitleArray[indexPath.row];
    cell.label.text = string;
    return cell;
    } else {
        if (indexPath.item == 0) {
            CVCellOfBelowReadArticle *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfBelowReadArticle" forIndexPath:indexPath];
            return cell;
        } else {
            CVCellOfSecondBelowReadArticle *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfSecondBelowReadArticle" forIndexPath:indexPath];
            cell.stringURL = _arrayOfStringURL[indexPath.row - 1];
            return cell;
        }
    }
}
#pragma mark - collectionView 被点击的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([collectionView isEqual:_upsideCollectionView]) {
        // 让小滑条跟着滑动
        [UIView transitionWithView:self.redSliderView duration:0.25 options:UIViewAnimationOptionLayoutSubviews animations:^{
            self.redSliderView.frame = CGRectMake(indexPath.row * 80 + 10, 40, 60, 4);
        } completion:^(BOOL finished) {
            
        }];
        // 让下面的 CollectionView 跟着动
        _belowCollectionView.contentOffset = CGPointMake(indexPath.row * SCREEN_WIDTH, 0);
    }
}
#pragma mark - collectionView 滑动方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isEqual:_belowCollectionView]) {
        // 让小滑条跟着滑动
        [UIView transitionWithView:self.redSliderView duration:0.25 options:UIViewAnimationOptionLayoutSubviews animations:^{
            self.redSliderView.frame = CGRectMake(scrollView.contentOffset.x / SCREEN_WIDTH * 80 + 10, 40, 60, 4);
        } completion:^(BOOL finished) {
            
        }];
        //连动效果
        if (self.redSliderView.frame.origin.x >= 80 * 4) {
                 _upsideCollectionView.contentOffset = CGPointMake(80 * 4 - 44 + (60 - 44), 0);
        } else {
            _upsideCollectionView.contentOffset = CGPointMake(_belowCollectionView.contentOffset.x / SCREEN_WIDTH * 80, 0);
        }
    }
}
#pragma mark - 其他,错误警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
